package com.rojored.controls.controlBarClasses 
{

import com.rojored.controls.progressDisplayClasses.IBufferProgressDisplay;
import flash.events.Event;

/**
 *  Adds buffer display capabilites to the PlayheadProgressDisplay component.
 *  
 *  @author gabriel montagné láscaris-comneno gabriel@rojored.com
 */
public class MediaProgressDisplay extends PlayheadProgressDisplay 
    implements IBufferProgressDisplay
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function MediaProgressDisplay() 
    {
        super();
        progressValues = Vector.<Number>([0, 0]);
    }

    //--------------------------------------------------------------------------
    //
    //  Properties: IBufferProgressDisplay
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    // bytesLoaded 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the bytesLoaded property
     */
    private var _bytesLoaded:Number;

    /**
     *  @private
     */
    private var bytesLoadedChanged:Boolean;

    [Bindable("bytesLoadedChanged")]

    /**
     *  @copy com.rojored.controls.progressDisplayClasses.IBufferProgressDisplay#bytesLoaded
     */
    public function get bytesLoaded():Number
    {
        return _bytesLoaded;
    }

    /**
     *  @private
     */
    public function set bytesLoaded(value:Number):void
    {
        if (_bytesLoaded == value)
            return;

        _bytesLoaded = value;
        bytesLoadedChanged = true;
        invalidateProperties();
        dispatchEvent(new Event("bytesLoadedChanged"));
    }

    //--------------------------------------
    // bytesTotal 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the bytesTotal property
     */
    private var _bytesTotal:Number;

    /**
     *  @private
     */
    private var bytesTotalChanged:Boolean;

    [Bindable("bytesTotalChanged")]

    /**
     *  @copy com.rojored.controls.progressDisplayClasses.IBufferProgressDisplay#bytesTotal
     */
    public function get bytesTotal():Number
    {
        return _bytesTotal;
    }

    /**
     *  @private
     */
    public function set bytesTotal(value:Number):void
    {
        if (_bytesTotal == value)
            return;

        _bytesTotal = value;
        bytesTotalChanged = true;
        invalidateProperties();
        dispatchEvent(new Event("bytesTotalChanged"));
    }

    //--------------------------------------------------------------------------
    //
    //  Overriden methods: UIComponent
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function commitProperties():void
    {
        if (bytesLoadedChanged || bytesTotalChanged)
        {
            bytesTotalChanged = false;
            bytesLoadedChanged = false;
            var bufferFraction:Number = 
                bytesTotal ? bytesLoaded / bytesTotal : 0;

            if (progressValues[1] != bufferFraction)
            {
                progressValues[1] = bufferFraction;
                invalidateDisplayList();
            }
        }
        super.commitProperties();
    }
}
}
